package com.luoxiang.socialandroid.activity

import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.lifecycle.lifecycleScope
import com.blankj.utilcode.util.LogUtils
import com.google.gson.Gson
import com.luoxiang.lib_common.common.Constants
import com.luoxiang.lib_common.common.Constants.MmkvConst.EXTRA
import com.luoxiang.lib_common.common.Constants.MmkvConst.HOSTAUDIT
import com.luoxiang.lib_common.common.Constants.MmkvConst.KEY
import com.luoxiang.lib_common.common.Constants.MmkvConst.TOKEN
import com.luoxiang.socialandroid.R
import com.luoxiang.socialandroid.base.BaseActivity
import com.luoxiang.socialandroid.bean.GetUserData
import com.luoxiang.socialandroid.bean.NumberBean
import com.luoxiang.socialandroid.utils.AESUtil
import com.luoxiang.socialandroid.utils.LoginUtil.Companion.initLogin
import com.luoxiang.socialandroid.utils.LoginUtil.Companion.startLogin
import com.luoxiang.socialandroid.utils.MmkvUtil
import com.luoxiang.socialandroid.utils.MmkvUtil.decodeString
import com.luoxiang.socialandroid.utils.NetUtils
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class StartActivity : BaseActivity() {
    private var isRead = false
    private var block: (() -> Unit)? = null
    override fun initListener() {
    }

    override fun initLayout(savedInstanceState: Bundle?) {
        setContentView(R.layout.activity_start)
        setStatusBarTranslucent()
        val TOKENS = MmkvUtil.decodeString(Constants.MmkvConst.TOKEN)
        val keybutezhi = MmkvUtil.decodeString("keybutezhi")
        getUserInfo()
        if (TOKENS != null && TOKENS.isNotEmpty() && keybutezhi == "ok") {
            block = {
                val intent = Intent(this@StartActivity, HomeActivity::class.java)
                startActivity(intent)
                finish()
            }
        } else if (TOKENS != null && TOKENS.isNotEmpty() && keybutezhi != "ok") {
            block = {
                val intent = Intent(this@StartActivity, Tezhi1Activity::class.java)
                startActivity(intent)
                finish()
            }
        } else {
            initLogin(
                this@StartActivity,
                { result: Boolean? ->
                    startLogin(this@StartActivity, result!!)
                    finish()
                    null
                },
                {
                    showLoading()
                    null
                }
            ) {
                clearLoading()
                null
            }
        }
        lifecycleScope.launch {
            delay(2000)
            isRead = true
            block?.invoke()
        }
    }

    override fun initView() {
    }

    private fun getUserInfo() {
        NetUtils.getInstance().apiService.getMineUserInfo(decodeString(TOKEN), EXTRA)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : io.reactivex.Observer<NumberBean> {
                override fun onSubscribe(d: Disposable?) {}

                override fun onError(e: Throwable?) {}

                override fun onComplete() {}

                override fun onNext(numberBean: NumberBean?) {
                    val data: String? = numberBean?.data
                    if (data?.isEmpty() == true) {
                        Log.e("StartActivity", "响应体为空")
                        return
                    }
                    try {
                        val decryptStr = AESUtil.aesDecryptString(data, KEY)
                        LogUtils.i("decryptStrpq", decryptStr)
                        val gson = Gson()
                        val data1 = gson.fromJson(decryptStr, GetUserData::class.java)
                        data1.data.user.let {
                            Log.d("StartActivity", "当前用户是否为主播:${it.host == 1}")
                            MmkvUtil.encode(HOSTAUDIT, it.host)
                        }
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
                }

            })
    }
}